package com.bawei.module_home.ui.devices

import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.adapter.DeviceAdapter
import com.bawei.module_home.databinding.ActivityDevicesBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.DeviceModel
import com.bumptech.glide.Glide
import dagger.hilt.android.AndroidEntryPoint

/**
 *author：yjk
 *description:
 *date：2025/6/12
*/
@AndroidEntryPoint
@Route(path = Router.DEVICES_PAGE)
class DevicesActivity : BaseActivity<BaseViewModel,ActivityDevicesBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_devices
    private val mViewModel : HomeServiceViewModel by viewModels()
    private lateinit var mAdapter: DeviceAdapter
    private var position_delete=0
    override fun initView() {
        super.initView()
        initTopBar()
        initAdapter()
        mBinding.etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
            override fun afterTextChanged(s: Editable?) {
                // 这里可以实现实时搜索（如联想词）
                 performSearch(s.toString())
            }
        })

    }
    private fun performSearch(query:String) {
//        val query = mBinding.etSearch.text.toString().trim()
        if (query.isNotEmpty()) {
            // 实际开发中，这里可以发起网络请求或本地搜索
            mAdapter.setData(mViewModel.deviceList.filter { it.deviceName.contains(query) })
        } else {
            // 如果搜索框为空，则显示所有数据
            mAdapter.setData(mViewModel.deviceList)
        }
    }
    fun initTopBar() {

        Glide.with(this).load(com.bawei.lib_common.R.drawable.back_other).into(mBinding.toolbar.back)
        Glide.with(this).load(com.bawei.lib_common.R.drawable.more).into(mBinding.toolbar.more)
        mBinding.toolbar.title.text = "设备管理"
        mBinding.toolbar.back.setOnClickListener {
            finish()
        }
        mBinding.toolbar.more.setOnClickListener {
            ARouter.getInstance().build(Router.INSERT_DEVICE_PAGE).navigation()
        }
    }

    override fun initData() {
        super.initData()
        mViewModel.getDeviceList()
    }

    override fun initListener() {
        super.initListener()
        mViewModel.deviceState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this,it.message)
                }
                Loading -> {}
                Success -> {
                    Log.d("YJK", "device: success")
                    mAdapter.setData(mViewModel.deviceList)
                }
            }
        }
        mViewModel.deleteDeviceState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this,"删除失败"+it.message)
                }
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"删除成功")
                    mAdapter.list.removeAt(position_delete)
                    mAdapter.notifyItemRemoved(position_delete)
                }
            }
        }
    }
    fun initAdapter(){
        mAdapter = DeviceAdapter().apply {
            setOnItemClickListener(object : DeviceAdapter.OnItemClickListener{
                override fun onItemClick(item: DeviceModel, position: Int) {
                    ARouter.getInstance().build(Router.DEVICE_DETAIL_PAGE).withInt("deviceId",item.id).navigation()
                }
            })
            setOnItemLongClickListener(object : DeviceAdapter.OnItemLongClickListener{
                override fun onItemLongClick(item: DeviceModel, position: Int) {
                    position_delete=position
                    mViewModel.deleteDevice(item.id)
                }
            })
        }
        mBinding.rvDevice.layoutManager = LinearLayoutManager(this)
        mBinding.rvDevice.adapter = mAdapter

    }

    override fun onResume() {
        super.onResume()
        mViewModel.getDeviceList()
    }

}